home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap18 / exer1803 / Clicker.java
Encoding:
Java Source  |  1997-04-20  |  1.1 KB  |  46 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.Serializable;
  4. import java.util.Vector;
  5.  
  6. public class Clicker extends Component 
  7.    implements Serializable, ActionListener
  8. {
  9.    private Vector listeners = new Vector();
  10.  
  11.    public void setBackground(Color c) {
  12.       super.setBackground( c );
  13.       repaint();
  14.    }
  15.  
  16.    public Color getBackground() {
  17.       return super.getBackground();
  18.    }
  19.  
  20.    public synchronized void addActionListener(ActionListener a) {
  21.       listeners.addElement(a);
  22.    }
  23.  
  24.    public synchronized void removeActionListener(ActionListener a) {
  25.       listeners.removeElement(a);
  26.    }
  27.  
  28.    public void actionPerformed(ActionEvent e) {
  29.       Vector targets;
  30.  
  31.       synchronized (this) {
  32.          targets = (Vector)listeners.clone();
  33.       }
  34.  
  35.       ActionEvent actionEvt = new ActionEvent(this, 0, null);
  36.       for (int i = 0; i < targets.size(); i++) {
  37.           ActionListener target = (ActionListener)targets.elementAt(i);
  38.           target.actionPerformed(actionEvt);
  39.       }
  40.    }
  41.  
  42.    public Dimension getMinimumSize() {
  43.       return new Dimension(100, 100);
  44.    }
  45. }
  46.